package ar.pmt.vista.jsonServlet.core;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;






import ar.pmt.vista.beans.ConstanteBean;
import ar.pmt.vista.beans.UtilBean;
import ar.pmt.vista.beans.VistaBean;

import com.google.gson.Gson;
import com.google.gson.JsonObject;


public class JsonUtils {
	
	public static String requestParametersToJson(Map<String, String[]> requestParameters){
		JsonObject jsonObject = new JsonObject();
		Iterator<String> iterator = requestParameters.keySet().iterator();
		while(iterator.hasNext()) {
			String key = (String) iterator.next();
			String value = requestParameters.get(key)[0];
			jsonObject.addProperty(key, value);
		    
		}
		return jsonObject.toString();
	}
	
	
	/**
	 * Metodo que transforma los atributos de la request correspondiente a una Clase A una cadena JSON.
	 * @param requestParameters Map de la request
	 * @param clase Filtro el cual solo transformara request a Json solo los atributos de dicha clase.
	 * @return Json String.
	 */
	public static String requestParametersToJson(Map<String, String[]> requestParameters, Class<?> clase){
		JsonObject jsonObject = new JsonObject();
		Iterator<String> iterator = requestParameters.keySet().iterator();
		while(iterator.hasNext()) {
			String claseAtributo = (String) iterator.next();
			int corte = claseAtributo.lastIndexOf(ConstanteBean.COMODIN);
			if(corte!=-1){
				String claseIterada = UtilBean.cambiarComodinPorPunto(claseAtributo.substring(0, corte));
				if(clase.getName().equals(claseIterada)){
					String key = claseAtributo.substring(corte+1);
					String value = requestParameters.get(claseAtributo)[0];
					jsonObject.addProperty(key, value);
				}
			}    
		}
		return jsonObject.toString();		
	}
	
	
	/**
	 * Metodo que retorna una Collection de clases distintas que se encuentran en el requestMap
	 * @param requestParameters
	 * @return Clases distintas.
	 */
	@SuppressWarnings("unchecked")
	public static Class<?>[] obtenerClases(Map<String, String[]> requestParameters){
		
		Class<?>[] clasesOut = new Class<?>[requestParameters.size()];
		int i = 0;
		Iterator<String> iterator = requestParameters.keySet().iterator();
		Set<String> clases = new HashSet<String>();
		while(iterator.hasNext()) {
			String claseAtributo = (String) iterator.next();
			int corte = claseAtributo.lastIndexOf(".");
			if(corte!=-1){
				String claseIterada = claseAtributo.substring(0, corte);
				if(!clases.contains(claseIterada)){
					clases.add(claseIterada);
					Class<?> c;
					try {
						c = Class.forName(claseIterada);
						clasesOut[i] = c;
						i++;
					} catch (ClassNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
		Class<?>[] clasesOutOK = new Class<?>[i];
		for (int j = 0; j < clasesOutOK.length; j++) {
			clasesOutOK[j] = clasesOut[j];
		}
		return clasesOutOK;		
	}
	
	
	public static String requestParametersToJson(String request){
		
		JsonObject jsonObject = new JsonObject();
		if(request==null){
			return jsonObject.toString();
		}
		while (request.indexOf("&")!=-1) {
			String parametro = request.substring(0, request.indexOf("&"));
			String key = parametro.substring(0, parametro.indexOf("="));
			String value = parametro.substring( parametro.indexOf("=")+1);
			jsonObject.addProperty(key, value);
			request = request.substring(request.indexOf("&")+1);
		}
		if(request.length()>0){
			String key = request.substring(0, request.indexOf("="));
			String value = request.substring( request.indexOf("=")+1);
			jsonObject.addProperty(key, value);
		}
		return jsonObject.toString();
		
	}
	
	@SuppressWarnings("unchecked")
	public static Object jsonToJava (String json, Class clase){
		Gson gson = new Gson();
		return gson.fromJson(json, clase);
		
	}
	
	public static String javaToJSON (Object objeto){
		Gson gson = new Gson();
		return gson.toJson(objeto);
		
	}

}
